package com.zahb.qadx.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import androidx.core.view.ViewCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.zahb.qadx.R;
import com.zahb.qadx.base.BaseActivityV2;
import com.zahb.qadx.base.BaseApplication;
import com.zahb.qadx.databinding.ActivityLecturerBinding;
import com.zahb.qadx.model.LecturerModel;
import com.zahb.qadx.net.RetrofitService;
import com.zahb.qadx.skeleton.RecyclerViewSkeletonScreen;
import com.zahb.qadx.skeleton.Skeleton;
import com.zahb.qadx.ui.adapter.LecturerAdapter;
import com.zahb.qadx.ui.view.decoration.MDividerItemDecoration;
import com.zahb.qadx.util.Constant;
import com.zahb.qadx.util.DisplayUtil;
import com.zahb.qadx.util.Tips;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by： huwei.
 * Data： 2020-10-22.
 * Time： 16:15.
 * Desc：全部讲师
 */
public class AllLecturerActivity extends BaseActivityV2 {
    Gson mGson = new Gson();
    List<LecturerModel.LecturerData.LecturerList> list = new ArrayList<>();
    RecyclerView recyclerView;
    SmartRefreshLayout refreshLayout;
    LecturerAdapter lecturerAdapter;

    private RecyclerViewSkeletonScreen mSkeletonScreen;
    private long mSkeletonShowTime;

    @Override
    public void initViewBinding() {
        ActivityLecturerBinding binding = ActivityLecturerBinding
                .inflate(getLayoutInflater(), mContainer, true);
        recyclerView = binding.recyclerView;
        refreshLayout = binding.refreshLayout;
    }

    @Override
    protected void initViews() {
        LinearLayoutManager managerGrid = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(managerGrid);
        recyclerView.addItemDecoration(new MDividerItemDecoration(getContext()));
        lecturerAdapter = new LecturerAdapter(getActivity(), list);
        recyclerView.setAdapter(lecturerAdapter);
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setOnRefreshListener(refreshLayout -> getData());
    }

    @Override
    protected int getTitleStringRes() {
        return R.string.all_lecturer;
    }

    @Override
    protected void onResume() {
        super.onResume();
        showSkeleton();
        getData();
    }

    private final int padding = DisplayUtil.dip2px2(16f);

    private void showSkeleton() {
        refreshLayout.setEnableRefresh(false);
        mSkeletonScreen = Skeleton.bind(recyclerView)
                .adapter(lecturerAdapter)
                .shimmer(true)
                .shimmerMode(RecyclerViewSkeletonScreen.SHIMMER_MODE_ITEM_CHILD)
                .angle(15)
                .frozen(true)
                //.color(R.color.black_333333)
                .duration(1000)
                .count(10)
                .load(R.layout.item_list_curriculum_skeleton)
                .show();
        ViewCompat.setPaddingRelative(recyclerView, padding, 0, padding, 0);
        mSkeletonShowTime = System.currentTimeMillis();
    }

    private void hideSkeleton() {
        if (mSkeletonScreen != null) {
            long currentTime = System.currentTimeMillis();
            if (currentTime - mSkeletonShowTime > Constant.MIN_NETWORK_TIME) {
                refreshLayout.setEnableRefresh(true);
                mSkeletonScreen.hide();
                mSkeletonScreen = null;
                ViewCompat.setPaddingRelative(recyclerView, 0, 0, 0, 0);
            } else {
                addDisposable(Observable.timer(mSkeletonShowTime + Constant.MIN_NETWORK_TIME - currentTime, TimeUnit.MILLISECONDS)
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(aLong -> hideSkeleton()));
            }
        }
    }

    private void getData() {
        Map<String, Object> params = new HashMap<>();
        params.put("pageNo", 1);
        params.put("pageSize", 99);
        params.put("requestType", 1);
        String json = mGson.toJson(params);
        RequestBody body = RequestBody.create(MediaType.parse("application/json"), json);
        if (!TextUtils.isEmpty(BaseApplication.getContext().getToken())) {
            addDisposable(RetrofitService.getNetService()
                    .getTeacherList(body)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(response -> {
                        refreshLayout.finishRefresh();
                        if (response.getStatusCode() == Constant.CODE_SUCCESS) {
                            lecturerAdapter.setList(response.getData().getList());
                        } else {
                            showBindToast(response.getErrorInfo());
                        }
                        hideSkeleton();
                    }, throwable -> {
                        refreshLayout.finishRefresh();
                        throwable.printStackTrace();
                        Tips.RequestError(getActivity());
                        hideSkeleton();
                    }));
        }
    }

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, AllLecturerActivity.class);
        context.startActivity(intent);
    }
}
